package com.gitee.feizns.explore.data_structure.array.easy;

/**
 * 628. 三个数的最大乘积
 * @author feizns
 * @since 2020/3/23
 */
public class MaximumProduct {

    public static void main(String[] args) {
        System.out.println(maximumProduct(new int[]{1, 2, 3, 4}));
    }

    public static int maximumProduct(int[] nums) {
        int max1 = nums[0];
        int max2 = Integer.MIN_VALUE;
        int max3 = Integer.MIN_VALUE;
        int min1 = max1;
        int min2 = Integer.MAX_VALUE;
        for (int i = 1; i < nums.length; i++) {
            if ( nums[i] > max1 ) {
                max3 = max2;
                max2 = max1;
                max1 = nums[i];
            } else if ( nums[i] > max2 ) {
                max3 = max2;
                max2 = nums[i];
            } else if ( nums[i] > max3 )
                max3 = nums[i];

            if ( nums[i] < min1 ) {
                min2 = min1;
                min1 = nums[i];
            } else if ( nums[i] < min2 )
                min2 = nums[i];
        }
        return Math.max(min1 * min2 * max1, max1 * max2 * max3);
    }

}
